package com.wanmait.baohan.controller.procurementBusinessController;

import com.wanmait.baohan.service.DictionaryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * TODO
 *
 * @author 打豆豆
 * @version 1.0
 * @date 2021/1/8 23:05
 * @description:
 */
@Controller
@RequestMapping("/purchase")
public class ProcurementTempleteController {
    @Resource
    private DictionaryService dictionaryService;



    /**
     * 打开创建新采购模板页面
     *
     * @param model
     * @return
     */
//    @RequestMapping("/procurementTemplate")
//    public String procurementTemplate(Model model) {
//        model.addAttribute("products", dictionaryService.getProducts());
//        return "initiatePurchaseProject";
//    }

    /**
     * 提交模板，提交模板文件
     *
     * @param file
     * @param procurementModel
     * @param productId
     * @param request
     * @return
     */
//    @RequestMapping("/submitProcurementTemplate")
//    @ResponseBody
//    public UploadVo submitProcurementTemplate(MultipartFile file, ProcurementModel procurementModel, Integer productId, HttpServletRequest request) {
//        UploadVo dataVo = new UploadVo();
//        try {
//            String ext = FilenameUtils.getExtension(file.getOriginalFilename());
//            String filename = procurementModel.getName() + "." + ext;
//            //String filepath =  request.getServletContext().getRealPath("/static/procurementTemplate/");
//            String filepath = ResourceUtils.getURL("classpath:").getPath() + "/static/file/procurementTemplate";
//            File newfile = new File(filepath, filename);
//            System.out.println(newfile.getAbsolutePath());
//            procurementModel.setContent(filename);
//            file.transferTo(newfile);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//            System.out.println("FileNotFoundException");
//            dataVo.setCode(1);
//            return dataVo;
//        } catch (IOException e) {
//            e.printStackTrace();
//            System.out.println("IOException");
//            dataVo.setCode(1);
//            return dataVo;
//        }
//
//        Product product = new Product();
//        product.setId(productId);
//        procurementModel.setStaff((Staff) request.getSession().getAttribute("user"));
//        procurementModel.setProduct(product);
//        procurementModelService.insert(procurementModel);
//        dataVo.setCode(0);
//        return dataVo;
//    }

    /**
     * hxn
     * @param id
     * @param model
     * @return
     * 打开模板详情
     */
//    @RequestMapping("procurementTemplateDetail")
//    public String procurementTemplateDetail(Integer id, Model model) {
//        model.addAttribute("procurementTemplate", procurementModelService.findById(id));
//        return "purchase/procurementTemplateDetail";
//    }

    @ResponseBody
    @RequestMapping("downloadContentFile")
    public void downloadContentFile(HttpServletRequest request, HttpServletResponse response, String filename) throws IOException {
        //设置文件MIME类型
        response.setContentType("application/octet-stream");
        //设置Content-Disposition
        response.setHeader("Content-Disposition", "attachment;filename=" + filename);
        //读取目标文件，通过response将目标文件写到客户端
        //获取目标文件的绝对路径
        String fullFileName = ResourceUtils.getURL("classpath:").getPath() + "static/file/procurementTemplate/" + filename;
        //读取文件
        InputStream in = new FileInputStream(fullFileName);
        OutputStream out = response.getOutputStream();
        //写文件
        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        in.close();
        out.flush();
        out.close();

    }

    @ResponseBody
    @RequestMapping("downloadRiskAssessmentFile")
    public void downloadRiskAssessmentFile(HttpServletRequest request, HttpServletResponse response, String filename) throws IOException {
        //设置文件MIME类型
        response.setContentType("application/octet-stream");
        //设置Content-Disposition
        response.setHeader("Content-Disposition", "attachment;filename=" + filename);
        //读取目标文件，通过response将目标文件写到客户端
        //获取目标文件的绝对路径
        String fullFileName = ResourceUtils.getURL("classpath:").getPath() + "static/file/procurementTemplate/" + filename;
        //读取文件
        InputStream in = new FileInputStream(fullFileName);
        OutputStream out = response.getOutputStream();
        //写文件
        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        in.close();
        out.flush();
        out.close();

    }



    //模式列表表格数据
//    @ResponseBody
//    @RequestMapping("/tableList")
//    public DataVo<ProcurementModel> tableList(Integer limit,Integer page,Integer enable){
//        DataVo dataVo =new DataVo();
//
//        PageInfo<ProcurementModel> list = procurementModelService.findList(page, limit,enable);
//
//        dataVo.setCount(list.getTotal());
//        dataVo.setData(list.getList());
//
//        return dataVo;
//    }

    //打开添加风险评估
    @RequestMapping("/addRisk")
    public void addRisk(Integer id,Model model) {
        model.addAttribute("id",id);
    }

    //添加风险评估
//    @RequestMapping("/addModelRisk")
//    @ResponseBody
//    public DataVo addModelRisk(MultipartFile file, Integer id, HttpServletRequest request) {
//        DataVo dataVo=new DataVo();
//        String filename="";
//        try {
//            String ext = FilenameUtils.getExtension(file.getOriginalFilename());
//
//            filename = UUID.randomUUID().toString() + "." + ext;
//            //String filepath =  request.getServletContext().getRealPath("/static/procurementTemplate/");
//            String filepath = ResourceUtils.getURL("classpath:").getPath() + "/static/file/riskAssessment";
//            File newfile = new File(filepath, filename);
//
//            file.transferTo(newfile);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//            System.out.println("FileNotFoundException");
//            dataVo.setMsg("FileNotFoundException");
//            return dataVo;
//        } catch (IOException e) {
//            e.printStackTrace();
//            System.out.println("IOException");
//            dataVo.setMsg("IOException");
//            return dataVo;
//        }
//
//        procurementModelService.insertRisk(id,filename);
//        dataVo.setMsg("yes");
//        return dataVo;
//    }
}